package com.weixiao.controller.api;

import com.weixiao.handler.YyghException;
import com.weixiao.helper.HttpRequestHelper;
import com.weixiao.model.hosp.Department;
import com.weixiao.model.hosp.Hospital;
import com.weixiao.model.hosp.Schedule;
import com.weixiao.result.Result;
import com.weixiao.result.ResultCodeEnum;
import com.weixiao.service.DepartmentService;
import com.weixiao.service.HospitalService;
import com.weixiao.service.HospitalSetService;
import com.weixiao.service.ScheduleService;
import com.weixiao.utils.ApiAssert;
import com.weixiao.utils.MD5;
import com.weixiao.vo.hosp.DepartmentQueryVo;
import com.weixiao.vo.hosp.ScheduleQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * author 韩诚
 * date 2022/9/20 10:58
 */
@Api(tags = "医院管理API接口")
@RestController
@RequestMapping("/api/hosp")
public class ApiController {

    @Autowired
    private HospitalService hospitalService;

    @Autowired
    private HospitalSetService hospitalSetService;

    @Autowired
    private DepartmentService departmentService;

    @Autowired
    private ScheduleService scheduleService;


    //================================医院接口=========================================

    /**
     * 上传医院
     * @param request
     * @return
     */
    @ApiOperation(value = "上传医院")
    @PostMapping("saveHospital")
    public Result saveHospital(HttpServletRequest request) {
        //获取传递过来的信息并使用工具类将其转换成object类型
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());

        //获取医院传递过来的签名，签名经过了MD5加密
        String hospSign = (String) paramMap.get("sign");

        //根据传递过来的医院编码，查询数据库的签名
        String hoscode = (String) paramMap.get("hoscode");
        String signKey = hospitalSetService.getSignKey(hoscode);

        //将数据库签名进行MD5加密
        String singKeyMD5 = MD5.encrypt(signKey);

        //判断签名是否一致
        ApiAssert.isTrue(hospSign.equals(singKeyMD5),ResultCodeEnum.SIGN_ERROR.getMessage());

        //传输过程中“+”转换为了“ ”，因此我们要转换回来
        String logoData = (String) paramMap.get("logoData");
        logoData = logoData.replaceAll(" ", "+");
        paramMap.put("logoData", logoData);

        //调用方法添加医院数据
        hospitalService.save(paramMap);
        return Result.ok();
    }

    /**
     * 获取医院信息
     * @param request
     * @return
     */
    @ApiOperation(value = "获取医院信息")
    @PostMapping("hospital/show")
    public Result getHospital(HttpServletRequest request) {
        //获取传递过来的信息并使用工具类将其转换成object类型
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());

        //获取医院传递过来的签名，签名经过了MD5加密
        String hospSign = (String) paramMap.get("sign");

        //根据传递过来的医院编码，查询数据库的签名
        String hoscode = (String) paramMap.get("hoscode");
        String signKey = hospitalSetService.getSignKey(hoscode);

        //将数据库签名进行MD5加密
        String singKeyMD5 = MD5.encrypt(signKey);

        //判断签名是否一致
        ApiAssert.isTrue(hospSign.equals(singKeyMD5),ResultCodeEnum.SIGN_ERROR.getMessage());

        //调用service方法实现根据医院编号查询
        Hospital hospital = hospitalService.getHospitalByHoscode(hoscode);
        return Result.ok(hospital);
    }

    //================================部门接口=========================================

    /**
     * 上传部门
     * @param request
     * @return
     */
    @ApiOperation(value = "上传部门")
    @PostMapping("saveDepartment")
    public Result saveDepartment(HttpServletRequest request) {
        //获取传递过来的信息并使用工具类将其转换成object类型
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());

        //获取医院传递过来的签名，签名经过了MD5加密
        String hospSign = (String) paramMap.get("sign");

        //根据传递过来的医院编码，查询数据库的签名
        String hoscode = (String) paramMap.get("hoscode");
        String signKey = hospitalSetService.getSignKey(hoscode);

        //将数据库签名进行MD5加密
        String singKeyMD5 = MD5.encrypt(signKey);

        //判断签名是否一致
        ApiAssert.isTrue(hospSign.equals(singKeyMD5),ResultCodeEnum.SIGN_ERROR.getMessage());

        //调用Service方法进行实现添加部门操作
        departmentService.save(paramMap);
        return Result.ok();
    }

    /**
     * 获取部门分页列表
     * @param request
     * @return
     */
    @ApiOperation(value = "获取部门分页列表")
    @PostMapping("department/list")
    public Result department(HttpServletRequest request) {
        //获取传递过来的科室信息
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
        //获取医院编号
        String hoscode = (String) paramMap.get("hoscode");

        //当前页和每页记录数
        int page = StringUtils.isEmpty(paramMap.get("page")) ? 1 : Integer.parseInt((String) paramMap.get("page"));
        int limit = StringUtils.isEmpty(paramMap.get("limit")) ? 1 : Integer.parseInt((String) paramMap.get("limit"));

        //获取医院传递过来的签名，签名经过了MD5加密
        String hospSign = (String) paramMap.get("sign");

        String signKey = hospitalSetService.getSignKey(hoscode);

        //将数据库签名进行MD5加密
        String singKeyMD5 = MD5.encrypt(signKey);

        //判断签名是否一致
        ApiAssert.isTrue(hospSign.equals(singKeyMD5),ResultCodeEnum.SIGN_ERROR.getMessage());

        //签名校验
        DepartmentQueryVo departmentQueryVo = new DepartmentQueryVo();
        departmentQueryVo.setHoscode(hoscode);

        //调用Service方法进行查询并返回查询结果
        Page<Department> pageModel = departmentService.selectPage(page, limit, departmentQueryVo);
        return Result.ok(pageModel);
    }

    /**
     * 删除科室
     * @param request
     * @return
     */
    @ApiOperation(value = "删除科室")
    @PostMapping("department/remove")
    public Result removeDepartment(HttpServletRequest request) {
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
        //获取医院编号
        String hoscode = (String) paramMap.get("hoscode");
        //获取科室编号
        String depcode = (String) paramMap.get("depcode");
        //获取医院传递过来的签名，签名经过了MD5加密
        String hospSign = (String) paramMap.get("sign");

        //获取数据库签名
        String signKey = hospitalSetService.getSignKey(hoscode);

        //将数据库签名进行MD5加密
        String singKeyMD5 = MD5.encrypt(signKey);

        //判断签名是否一致
        ApiAssert.isTrue(hospSign.equals(singKeyMD5),ResultCodeEnum.SIGN_ERROR.getMessage());

        departmentService.remove(hoscode, depcode);
        return Result.ok();
    }

    //================================部门排班接口=========================================

    /**
     * 上传排班
     * @param request
     * @return
     */
    @ApiOperation(value = "上传排班")
    @PostMapping("saveSchedule")
    public Result saveSchedule(HttpServletRequest request) {
        //获取传递过来的信息并使用工具类将其转换成object类型
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());

        //获取医院传递过来的签名，签名经过了MD5加密
        String hospSign = (String) paramMap.get("sign");

        //根据传递过来的医院编码，查询数据库的签名
        String hoscode = (String) paramMap.get("hoscode");
        String signKey = hospitalSetService.getSignKey(hoscode);

        //将数据库签名进行MD5加密
        String singKeyMD5 = MD5.encrypt(signKey);

        //判断签名是否一致
        ApiAssert.isTrue(hospSign.equals(singKeyMD5),ResultCodeEnum.SIGN_ERROR.getMessage());

        //调用service方法实现添加排班
        scheduleService.save(paramMap);

        return Result.ok();
    }

    /**
     * 获取排班分页列表
     * @param request
     * @return
     */
    @ApiOperation(value = "获取排班分页列表")
    @PostMapping("schedule/list")
    public Result schedule(HttpServletRequest request) {
        //获取传递过来的科室信息
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
        //获取医院编号和部门编号
        String hoscode = (String) paramMap.get("hoscode");

        //当前页和每页记录数
        int page = StringUtils.isEmpty(paramMap.get("page")) ? 1 : Integer.parseInt((String) paramMap.get("page"));
        int limit = StringUtils.isEmpty(paramMap.get("limit")) ? 1 : Integer.parseInt((String) paramMap.get("limit"));

        //获取医院传递过来的签名，签名经过了MD5加密
        String hospSign = (String) paramMap.get("sign");

        String signKey = hospitalSetService.getSignKey(hoscode);

        //将数据库签名进行MD5加密
        String singKeyMD5 = MD5.encrypt(signKey);

        //判断签名是否一致
        ApiAssert.isTrue(hospSign.equals(singKeyMD5),ResultCodeEnum.SIGN_ERROR.getMessage());

        //签名校验
        ScheduleQueryVo scheduleQueryVo = new ScheduleQueryVo();
        scheduleQueryVo.setHoscode(hoscode);

        //调用Service方法进行查询并返回查询结果
        Page<Schedule> pageModel = scheduleService.selectPage(page, limit, scheduleQueryVo);
        return Result.ok(pageModel);
    }

    /**
     * 删除排班
     * @param request
     * @return
     */
    @ApiOperation(value = "删除排班")
    @PostMapping("schedule/remove")
    public Result removeSchedule(HttpServletRequest request) {
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
        //获取医院编号
        String hoscode = (String) paramMap.get("hoscode");
        //获取排班编号
        String hosScheduleId = (String) paramMap.get("hosScheduleId");
        //获取医院传递过来的签名，签名经过了MD5加密
        String hospSign = (String) paramMap.get("sign");

        //获取数据库签名
        String signKey = hospitalSetService.getSignKey(hoscode);

        //将数据库签名进行MD5加密
        String singKeyMD5 = MD5.encrypt(signKey);

        //判断签名是否一致
        ApiAssert.isTrue(hospSign.equals(singKeyMD5),ResultCodeEnum.SIGN_ERROR.getMessage());

        scheduleService.remove(hoscode, hosScheduleId);
        return Result.ok();
    }
}